package com.ggh.jinjilive.ui.home.voice;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ConvertUtils;
import com.ggh.jinjilive.BaseApplication;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.FragmentVoiceBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.mobile.home.live.LiveCategory;
import com.ggh.jinjilive.mobile.home.voice.VoiceHot;
import com.ggh.jinjilive.pagestatus.EmptyCallback;
import com.ggh.jinjilive.pagestatus.LoadingCallback;
import com.ggh.jinjilive.ui.BaseFragment;
import com.ggh.jinjilive.ui.home.voice.adapter.VoiceAdapter;
import com.ggh.jinjilive.ui.home.voice.adapter.VoiceAdapter1;
import com.ggh.jinjilive.ui.home.voice.adapter.VoiceTypeAdapter;
import com.ggh.jinjilive.views.DividerItemDecoration1;
import com.ggh.jinjilive.views.GridItemDecoration;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadSir;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.MaterialHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class VoiceFragment extends BaseFragment<FragmentVoiceBinding> implements OnRefreshListener, OnLoadMoreListener {
    private VoiceViewModel voiceViewModel;
    //适配器
    private VoiceTypeAdapter voiceTypeAdapter;
    //热门适配器
    private VoiceAdapter voiceAdapter;
    //列表适配器
    private VoiceAdapter1 voiceAdapter1;

    @Override
    public void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseApplication.getInstance().getAppComponent().doInjectionMain(this);
        voiceViewModel = new ViewModelProvider(this,viewModelFactory).get(VoiceViewModel.class);
    }

    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        View contentView = binding.getRoot().findViewById(R.id.refreshLayout);
        loadService = LoadSir.getDefault().register(contentView, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadService.showCallback(LoadingCallback.class);
                onRefresh(binding.refreshLayout);
            }
        });
        loadService.showCallback(LoadingCallback.class);
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        return R.layout.fragment_voice;
    }

    @Override
    public void initView() {
        super.initView();
        binding.setVoiceFragment(voiceViewModel);

        binding.refreshLayout.setRefreshHeader(new MaterialHeader(mContext));
        binding.refreshLayout.setRefreshFooter(new ClassicsFooter(mContext));
    }

    @Override
    public void initData() {
        super.initData();
        //语音房类目
        voiceTypeAdapter = new VoiceTypeAdapter(mContext);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
        binding.liveTypeList.setLayoutManager(linearLayoutManager);
        binding.liveTypeList.setAdapter(voiceTypeAdapter);
        voiceTypeAdapter.setAddItemOnClickListener(new AddItemOnClickListener() {
            @Override
            public void onSkipClicked(int position, Object mObject) {
                LiveCategory liveCategory = (LiveCategory) mObject;
                if(position == 0){
                    voiceViewModel.cate_id.set("0");
                }else {
                    if(null != liveCategory){
                        voiceViewModel.cate_id.set(liveCategory.getId());
                    }
                }
                onRefresh(binding.refreshLayout);
            }
        });
        //语音放分类
        voiceViewModel.voiceCateList();
        voiceViewModel.listMutableLiveData.observe(this, new Observer<List<LiveCategory>>() {
            @Override
            public void onChanged(List<LiveCategory> liveCategories) {
                liveCategories.add(0,null);
                voiceTypeAdapter.setAdd(liveCategories);
                onRefresh(binding.refreshLayout);
            }
        });

        //三排
        GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext,3);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                int itemViewType = voiceAdapter.getItemViewType(position);
                if (itemViewType == voiceAdapter.TYPE_FOOTER) {
                    return 1;
                } else {
                    return 3;
                }
            }
        });
        binding.rootRecyclerView.setLayoutManager(gridLayoutManager);
        binding.rootRecyclerView.addItemDecoration(new DividerItemDecoration1(mContext));
        voiceAdapter = new VoiceAdapter();
        voiceAdapter.setAddItemOnClickListener(new AddItemOnClickListener() {
            @Override
            public void onSkipClicked(int position, Object mObject) {

            }
        });
        binding.rootRecyclerView.setAdapter(voiceAdapter);
//        //双排
        binding.rootRecyclerView1.setLayoutManager(new GridLayoutManager(getActivity(),2));
        binding.rootRecyclerView1.addItemDecoration(new GridItemDecoration(2, ConvertUtils.dp2px(10),false));
        voiceAdapter1 = new VoiceAdapter1();
        voiceAdapter1.setAddItemOnClickListener(new AddItemOnClickListener() {
            @Override
            public void onSkipClicked(int position, Object mObject) {

            }
        });
        binding.rootRecyclerView1.setAdapter(voiceAdapter1);

        //语音房列表
        voiceViewModel.mVoiceHot.observe(this, new Observer<List<VoiceHot>>() {
            @Override
            public void onChanged(List<VoiceHot> voiceHots) {
                binding.refreshLayout.finishRefresh();
                if(null != voiceHots){
                    if(voiceViewModel.isRefresh){
                        if(voiceHots.size() > 0){
                            loadService.showSuccess();
                            binding.refreshLayout.finishLoadMore();
                            binding.rootRecyclerView.setVisibility(View.VISIBLE);
                            binding.rootRecyclerView1.setVisibility(View.GONE);
                            if(voiceViewModel.cate_id.get().equals("0")){
                                List<VoiceHot> voiceHotList = new ArrayList<>();
                                List<VoiceHot> voiceHotList1 = new ArrayList<>();
                                for (int i = 0; i < voiceHots.size(); i++){
                                    if(i > 1){
                                        voiceHotList.add(voiceHots.get(i));
                                    }else {
                                        voiceHotList1.add(voiceHots.get(i));
                                    }
                                }
                                voiceHotList.add(0,null);
                                voiceAdapter.setAdd(voiceHotList);
                                voiceAdapter.setAdd1(voiceHotList1);

                            }else {
                                binding.rootRecyclerView.setVisibility(View.GONE);
                                binding.rootRecyclerView1.setVisibility(View.VISIBLE);
                                voiceAdapter1.setAdd(voiceHots);
                            }
                        }else {
                            loadService.showCallback(EmptyCallback.class);
                        }
                    }else {
                        if(voiceViewModel.cate_id.get().equals("0")){
                            if(voiceHots.size() > 0){
                                binding.refreshLayout.finishLoadMore();
                                voiceAdapter.Add(voiceHots);
                            }else {
                                binding.refreshLayout.finishLoadMoreWithNoMoreData();
                            }
                        }else {
                            if(voiceHots.size() > 0){
                                binding.refreshLayout.finishLoadMore();
                                voiceAdapter1.Add(voiceHots);
                            }else {
                                binding.refreshLayout.finishLoadMoreWithNoMoreData();
                            }
                        }

                    }
                }else {
                    binding.refreshLayout.finishLoadMore();
                }
            }
        });
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        //刷新
        binding.refreshLayout.setOnRefreshListener(this);
        //加载
        binding.refreshLayout.setOnLoadMoreListener(this);
    }

    @Override
    public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
        voiceViewModel.isRefresh = true;
        if(voiceViewModel.cate_id.get().equals("0")){
            voiceViewModel.hotList();
        }else {
            voiceViewModel.voiceList();
        }
    }

    @Override
    public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
        voiceViewModel.isRefresh = false;
        if(voiceViewModel.cate_id.get().equals("0")){
            voiceViewModel.hotList();
        }else {
            voiceViewModel.voiceList();
        }
    }


}
